﻿Imports System.Data.SqlClient

Public Class Dashboard_old
    Dim common_conn As New common
    Dim NewConString = ConnectDatabase.GetRegistryValue("stringcon")
    Private Sub Dashboard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim checkc
        checkc = common_conn.CheckConnection(NewConString)

        If checkc = "" Then

            MsgBox("Connected to database successfully!")
            SetUpListViewColumns()
            common_conn.Load_trees()

        Else
            MsgBox("Oops!! Seems like the database connection is lost.", MsgBoxStyle.Exclamation)
            If ConnectDatabase.Visible = False Then
                ConnectDatabase.ShowDialog()
            End If
        End If

    End Sub

    Private Sub New_test_suite_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles New_test_suite.Click
        Add_New_Test_Suite.ShowDialog()
        Add_New_Test_Suite.TST_Text.Text = ""
        Add_New_Test_Suite.TSD_Text.Text = ""
    End Sub

    Private Sub ExpandToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExpandToolStripMenuItem.Click
        TreeView1.SelectedNode.Expand()
    End Sub

    Private Sub Delete_node_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Delete_node.Click
        ' Removes currently selected node
        Dim getparentnodetoselect, getdeletednode, getdeletednodeID
        getparentnodetoselect = TreeView1.SelectedNode.Parent
        getdeletednode = TreeView1.SelectedNode.Text
        'MsgBox("To be deleted: " & getdeletednode)
        'MsgBox("ID of deleted node: " & GetIdofnode(getdeletednode))
        getdeletednodeID = GetIdofnode(getdeletednode)
        'MsgBox("Baap of deleted node: " & getparentnodetoselect.text)
        Dim response As MsgBoxResult
        response = MsgBox("Are you sure you want to delete? All data under this node will be lost.", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Deleting " & TreeView1.SelectedNode.Text)
        If response = MsgBoxResult.Yes Then
            Add_New_Test_Suite.Execute_commands("DELETE FROM Tree_Views WHERE Id = '" & getdeletednodeID & "'")
            Add_New_Test_Suite.Execute_commands("DELETE FROM Tree_Views WHERE Node_Level = '" & getdeletednodeID & "'")
            common_conn.Load_trees()
            TreeView1.ExpandAll()
            TreeView1.SelectedNode = getparentnodetoselect
        ElseIf response = MsgBoxResult.No Then
            Exit Sub
        End If
    End Sub

    Private Sub ContextMenuStrip_root_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ContextMenuStrip_root.Opening

        If TreeView1.TopNode.IsSelected Then
            Delete_node.Enabled = False
            Edit_node.Enabled = False
        Else
            Delete_node.Enabled = True
            Edit_node.Enabled = True
        End If

    End Sub




    Private Sub SetUpListViewColumns()
        'List Column Headers
        Dim lvColumnHeader As ColumnHeader

        lvColumnHeader = ListView1.Columns.Add("#")
        lvColumnHeader.Width = 50
        lvColumnHeader = ListView1.Columns.Add("Title")
        lvColumnHeader.Width = 100
        lvColumnHeader = ListView1.Columns.Add("Decription")
        lvColumnHeader.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize)
    End Sub

    'Public Sub load_columns(ByVal command As String)
    '    'Load respective data in columns

    '    Dim conn As New SqlClient.SqlConnection
    '    conn.ConnectionString = "data source=cc-zshaikh\sqlexpress;initial catalog=test_case_db;user id=sa;password=ccmd5#238;pooling=false"
    '    Dim myinsertquery As String = command
    '    Dim mycommand As New SqlCommand(myinsertquery)
    '    mycommand.Connection = conn
    '    conn.Open()
    '    Dim reader As SqlDataReader = mycommand.ExecuteReader()
    '    While reader.Read()
    '        Dim lvcolumnheader As ColumnHeader
    '        lvcolumnheader = ListView1.Columns.Add(reader(0))
    '        lvcolumnheader.Width = 100
    '    End While

    'End Sub


    Public Sub Load_view(ByVal command As String)
        'Load respective data in columns
        Dim conn As New SqlClient.SqlConnection

        conn.ConnectionString = NewConString
        Dim myInsertQuery As String = command

        Dim myCommand As New SqlCommand(myInsertQuery)
        myCommand.Connection = conn

        conn.Open()
        Dim reader As SqlDataReader = myCommand.ExecuteReader()
        Dim lvItem As ListViewItem

        Do
            While reader.Read()

                lvItem = ListView1.Items.Add(ListView1.Items.Count + 1)
                lvItem.SubItems.AddRange(New String() {reader(1), reader(2)})
                lvItem.ImageIndex = 1

            End While
        Loop While reader.NextResult()

    End Sub 'SelectSqlClientSrvRows


    Private m_SortingColumn As ColumnHeader


    Private Sub ListView1_ColumnClick(ByVal sender As Object, ByVal e As System.Windows.Forms.ColumnClickEventArgs) Handles ListView1.ColumnClick
        ' Get the new sorting column.  
        Dim new_sorting_column As ColumnHeader = ListView1.Columns(e.Column)
        ' Figure out the new sorting order.  
        Dim sort_order As System.Windows.Forms.SortOrder
        If m_SortingColumn Is Nothing Then
            ' New column. Sort ascending.  
            sort_order = SortOrder.Ascending
        Else ' See if this is the same column.  
            If new_sorting_column.Equals(m_SortingColumn) Then
                ' Same column. Switch the sort order.  
                If m_SortingColumn.ImageIndex = 3 Then
                    sort_order = SortOrder.Descending
                Else
                    sort_order = SortOrder.Ascending
                End If
            Else
                ' New column. Sort ascending.  
                sort_order = SortOrder.Ascending
            End If
            ' Remove the old sort indicator.  
            m_SortingColumn.ImageIndex = 5


        End If
        ' Display the new sort order.  
        m_SortingColumn = new_sorting_column
        If sort_order = SortOrder.Ascending Then
            m_SortingColumn.ImageIndex = 3
        Else
            m_SortingColumn.ImageIndex = 4
        End If
        ' Create a comparer.  
        ListView1.ListViewItemSorter = New clsListviewSorter(e.Column, sort_order)
        ' Sort.  
        ListView1.Sort()
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        ListView1.Items.Clear()
        Load_view("SELECT * from tree_views where Node_Level = '" & Add_New_Test_Suite.GetLastId() & "' ORDER BY id")

    End Sub


    Private Sub RefeshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        SetUpListViewColumns()
        common_conn.Load_trees()
        TreeView1.SelectedNode = TreeView1.TopNode
    End Sub

    Private Sub TreeView1_NodeMouseClick1(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
        If (e.Button = MouseButtons.Right) Then
            TreeView1.SelectedNode = e.Node
        End If
    End Sub


    Public Function GetIdofnode(ByVal nodename As String)

        Dim conn As New SqlClient.SqlConnection
        ' todo: modify the connection string and include any
        ' additional required properties for your database.
        conn.ConnectionString = NewConString
        Dim myinsertquery As String = ("select Id from Tree_Views where Node_Title = '" & nodename & "'")
        Dim mycommand As New SqlCommand(myinsertquery)
        mycommand.Connection = conn
        conn.Open()
        Dim reader As SqlDataReader = mycommand.ExecuteReader()
        While reader.Read()
            GetIdofnode = (reader(0).ToString)
        End While

    End Function


End Class
